<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class MediaBrowserXls
{

  public static function saveToDb($filename)
  {
    try {
      error_reporting(E_ALL ^ E_NOTICE);
      $data = new sfExcelReader($filename);
      $sheet = 0;
      $upload_date = $data->val(2,2,$sheet);
      for($row=3;$row<=$data->rowcount($sheet);$row++) {
          $lastname= null;
          $firstname= null;
          $mi= null;
          $email= null;
          $fb= null;
          $category= null;
          $bib = null;
          $mobile_no = null;
          for($col=1;$col<=$data->colcount($sheet);$col++) {
            $val = $data->val($row,$col,$sheet);
            if ($val=='') { continue; }
            if($col == 1) {
              $lastname = mb_convert_case($val, MB_CASE_TITLE, "UTF-8");
            } elseif ($col == 2) {
              $firstname=mb_convert_case($val, MB_CASE_TITLE, "UTF-8");
            } elseif ($col == 3) {
              $mi= strtoupper($val);
            } elseif ($col == 4) {
              $email=strtolower($val);
            } elseif ($col == 5) {
              $fb=strtolower($val);
            } elseif ($col == 6) {
              $category=$val;
            } elseif ($col == 7) {
              $bib=$val;
            } elseif ($col == 8) {
              $mobile_no=$val;
            }
          }
          if (!RunnersListTable::isExist($lastname, $firstname, $mi, $bib)) {
            $user = sfGuardUserProfile::findByNameAndEmail($first_name, $last_name, $fb);
            $runner = new RunnersList();
            if( $user instanceof  sfGuardUserProfile) {
              $runner->insertRunners($lastname, $firstname, $mi, $email, $fb, $category, $bib, $upload_date, $mobile_no, $user->getUserId());
            } else {
              $runner->insertRunners($lastname, $firstname, $mi, $email, $fb, $category, $bib, $upload_date, $mobile_no);
            }
          }
      }
      return;
    } catch (Exception $exc) {
      die($exc->getMessage());
      return '{"jsonrpc" : "2.0", "error" : {"code": 999, "message": "Failed to save to database."}, "id" : "id"}';
    }
  }
}
?>